package szzc.sdk.http;

import java.util.HashMap;

import szzc.sdk.util.StringUtil;

/**
 * Created by lianbin.xu@ucarinc.com on 2017/9/19.
 */

public class HttpRequest {
    private static final int METHOD_MIN =0;
    public static final int METHOD_GET = 1;
    public static final int METHOD_POST = 2;
    public static final int METHOD_PUT = 3;
    private static final int METHOD_MAX =4;

    private Builder builder;

    HttpRequest(Builder builder){
        this.builder = builder;
    }

   public HashMap<String, String> getHeaders(){
       return new HashMap<String, String>(builder.headers);
   }

   public  HashMap<String, String> getParameters(){
       return new HashMap<String, String>(builder.parameters);
   }

   public String getURL(){
       return builder.url;
   }

   public int getMethod(){
       return builder.method;
   }


    public static class Builder{
        private HashMap<String, String> headers = new HashMap<>();
        private HashMap<String, String> parameters = new HashMap<>();
        private String url;
        private int method = -1;
        public Builder(){

        }

        public Builder(String url){
            this.url = url;
        }

        public Builder setHeaders(HashMap<String, String> httpHeaders){
            headers.putAll(httpHeaders);
            return this;
        }
        public Builder setParameters( HashMap<String, String> httpParameters){
            this.parameters.putAll(httpParameters);
            return this;
        }
        public Builder setURL(String url){
            this.url = url;
            return this;
        }

        public Builder setMethod(int method){
            this.method = method;
            return this;
        }

        public HttpRequest build(){
            validateState();
            return new HttpRequest(this);
        }

        private void validateState() {
            if(StringUtil.isEmpty(url)){
                throw new IllegalArgumentException("URL shall not be empty");
            }
            if(method <= METHOD_MIN || method >= METHOD_MAX){
                throw new IllegalArgumentException("Invalid http method:" + method);
            }
        }

    }

}
